package com.personnalManagerSystem.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.personnalManagerSystem.model.*;
import com.personnalManagerSystem.service.IUserService;
import com.personnalManagerSystem.util.Md5Util;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liqing
 * @since 2022-03-27
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    //用户列表
    //数据库的维护刷新一下
    @RequestMapping("/list")
    public String list(Model model,
                       String option,
                       String searchContent,
                       Integer pageNum,
                       Integer pageSize){
        //调用封装好的数据处理器
        baseList(model,option,searchContent,pageNum,pageSize,iUserService);
        //一个用户对应多个角色,查询角色列表,(分配角色的模态框中要使用)
        List<Role> roleList = iRoleService.list();
        model.addAttribute("roleList",roleList);
        //跳转页面
        return "view/user";
    }

    //添加用户校验用户名
    @RequestMapping("/checkUsername")
    @ResponseBody //第二次犯错了,注解注解
    public Map<String,Object> checkUsenname(@RequestParam("username") String username){
        System.out.println(username);
        Map<String,Object> map= new HashMap<>();
        User user = iUserService.queryUsername(username);
        System.out.println("user:"+user);
        if(user!=null){
            //说明此用户名已经被别人所使用过,那么状态就设置为201
            System.out.println("---------------");
            map.put("code",201);
            map.put("msg","该登录名已经被使用过,请重新输入");

        }else{
            map.put("code",200);
            map.put("msg","请继续操作吧");
        }
        return map;
    }
    @RequestMapping("/add")
    public String add(User user){
       // System.out.println("user:"+user);
        //在Md5Util工具类中,随机生成16位字母组成盐,这里需要进行调用,且使用了MD5进行加密,也应该对密码进行转换,否则下次登录出错
        String salt = Md5Util.createRandomStr();
        user.setSalt(salt);
        //调用Md5Util工具类生成加密之后的密码
        String password = Md5Util.md5(salt, user.getPassword());
        user.setPassword(password);
        iUserService.save(user);
        //直接重定向就不用多次写查询数据,封装数据,避免代码冗余
        //return "redirect:list";
        //程序全部使用绝对路径
        return "redirect:/user/list";
    }
    @RequestMapping("/edit")
    public String edit(User user){
        System.out.println("user:"+user);
        //不用修改盐值,仍旧使用原来的盐值
        //如果不修改密码并不会对密码造成什么影响,如果修改密码,直接用原来的盐值对修改之后的密码进行加密即可
        String password = Md5Util.md5(user.getSalt(), user.getPassword());
        user.setPassword(password);
        iUserService.updateById(user);
        return "redirect:/user/list";
    }

    @RequestMapping("/del")
    public String del(@RequestParam("userId")Integer userId){
        //System.out.println("userId:"+userId);
        iUserService.removeById(userId);
        return "redirect:/user/list";
    }




    //对于显示用户的部门信息职位表
    public  Model baseList1(Model model,
                            String option,
                            String searchContent,
                            Integer pageNum,
                            Integer pageSize,
                            IUserService iUserService){
        //设置默认的页码
        if(pageNum==null ||pageNum==0){
            pageNum=1;
        }
        //设置默认的一页显示数据
        if(pageSize==null || pageSize==0){
            pageSize=5;
        }

        QueryWrapper<User2> wrapper = new QueryWrapper<>();
        if(option!=null
                && searchContent!=null
                && !"".equals(option)
                && !"".equals(searchContent)){
            wrapper.like(option,searchContent);
        }

        //设置分页
        Page<List<User2>> pageInfo = PageHelper.startPage(pageNum, pageSize);
        //查询数据
        iUserService.selectUserDepartmentPosition(wrapper);
        //设置页面信息
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("option",option);
        model.addAttribute("searchContent",searchContent);
        return model;
    }


    //用户信息里面里面包含了部门信息,职位信息,这个还是写在用户控制层中,以后调用更加方便
    @RequestMapping("/list1")
    public String list1(Model model,
                        String option,
                        String searchContent,
                        Integer pageNum,
                        Integer pageSize){
        List<Position> positionList = iPositionService.list();
        model.addAttribute("positionList",positionList);
        List<Department> departmentList = iDepartmentService.list();
        model.addAttribute("departmentList",departmentList);
        baseList1(model,option,searchContent,pageNum,pageSize,iUserService);
        return  "view/userDepartmentPosition";
    }
    @RequestMapping("/edit1")
    public String edit1(@RequestParam(value = "userId",required = false)Integer userId,
                        @RequestParam(value = "departmentId",required = false)Integer departmentId,
                        @RequestParam(value = "positionId",required = false)Integer positionId
                        ){
//        System.out.println("userId:"+userId);
//        System.out.println("departmentId:"+departmentId);
//        System.out.println("positionId:"+positionId);
        //维护用户表中的数据
        User user = new User();
        user.setPositionId(positionId);
        user.setUserId(userId);
        iUserService.saveOrUpdate(user);
        //维护用户部门表数据
        UserDepartment userDepartment = new UserDepartment();
        userDepartment.setUserId(userId);
        userDepartment.setDepartmentId(departmentId);
        iUserDepartmentService.saveOrUpdate(userDepartment);
        //维护部门职位表中的数据
        DepartmentPosition departmentPosition = new DepartmentPosition();
        departmentPosition.setDepartmentId(departmentId);
        departmentPosition.setPositionId(positionId);
        iDepartmentPositionService.saveOrUpdate(departmentPosition);
        return "redirect:/user/list1";
    }


}

